<template>
  <div>
    <div class="classify">
      <div class="top_bar">
        <div class="tabs">
          <!-- 标签页 -->
          <van-tabs
            v-model="direction"
            background="#fff"
            :line-width="0"
            :line-height="0"
            @click="changeDirection"
          >
            <!-- 遍历每一个标签 -->
            <van-tab
              v-model="direction"
              :title="channel.name"
              v-for="(channel, index) in classifyData"
              :key="index"
            >
              <!-- 遍历类型 -->
              <div class="choice_type">
                <div
                  class="type"
                  :class="[{ active: index == choicetype }]"
                  v-for="(type, index) in channel.cates"
                  :key="index"
                >
                  <span @click="clickType(index, type.url)">{{
                    type.name
                  }}</span>
                </div>
              </div>
              <div class="book_data">
                <div class="loading" v-if="!bookAlldata">
                  <van-loading
                    size="30px"
                    text-size="30px"
                    text-color="#adadad"
                  />Loding....
                </div>
                <div class="loading" v-if="!bookAlldata">
                  <van-loading
                    size="30px"
                    text-size="30px"
                    text-color="#adadad"
                  />Loding....
                </div>
                <!--过滤器 -->
                <div class="filter">
                  <!-- 畅销,新书 -->
                  <div class="new_hot">
                    <span
                      @click="changeHotNew(1, currentUrl)"
                      :class="{ active: hotNew == 1 }"
                      >畅销</span
                    >
                    <span
                      @click="changeHotNew(3, currentUrl)"
                      :class="{ active: hotNew == 3 }"
                      >最新</span
                    >
                  </div>
                  <!-- 按钮 -->
                  <div class="btn">
                    <div class="icon">
                      <span class="iconfont icon-xiajiantou"></span>
                    </div>
                    筛选
                  </div>
                </div>
                <!-- 遍历出书书籍的内容 -->
                <ul>
                  <router-link
                    tag="li"
                    :to="{
                      name: 'comment',
                      query: { id: data.id, title: data.title },
                    }"
                    v-for="data in bookAlldata"
                    :key="data.id"
                  >
                    <div class="img">
                      <img :src="data.cover" alt="" />
                    </div>
                    <div class="book_name">
                      <p>{{ data.title }}</p>
                      <!-- <span>496章未读</span> -->
                    </div>
                  </router-link>
                </ul>
              </div>
            </van-tab>
          </van-tabs>
          <span class="fdj iconfont icon-Magnifier"></span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { getAllChannel, getclassifyResult } from "../../api/classify";
export default {
  data() {
    return {
      // 第一个选择
      direction: 0,
      // 第二个选择
      choicetype: 0,
      // 第三个
      hotNew: 1,
      // allTypes: [],
      // 全部数据
      classifyData: null,
      // 渲染全部书数据
      bookAlldata: null,
      // 保存当前url
      currentUrl: null,
    };
  },
  created() {
    this.getAllChannelFun();
    console.log(this.classifyData);
  },
  methods: {
    changeDirection() {
      this.choicetype = 0;
      this.currentUrl = this.classifyData[this.direction].cates[0].url;
      this.getChannelTypeUrl(this.currentUrl);
    },
    changeHotNew(type, url) {
      this.hotNew = type;
      this.currentUrl = url;
      this.getChannelTypeUrl(url);
    },
    getChannelTypeUrl(url) {
      // this.bookAlldata = null;
      // 直接更改url
      let newUrl = url.replace("cat.json", "searchBook.json");
      getclassifyResult({ url: newUrl, sort: this.hotNew })
        .then(({ data: res }) => {
          this.bookAlldata = res.list;
        })
        .catch((err) => {
          if (err) {
            this.$toast({
              message: "服务器崩溃",
              position: "middle",
            });
          }
        });
    },
    getAllChannelFun() {
      getAllChannel()
        .then(({ data: res }) => {
          this.classifyData = res;
          this.currentUrl = this.classifyData[0].cates[0].url;
          this.getChannelTypeUrl(this.currentUrl);
        })
        .catch((err) => {
          if (err) {
            this.$toast({
              message: "服务器崩溃",
              position: "middle",
            });
          }
        });
    },
    clickType(type, url) {
      this.choicetype = type;
      // console.log(url);
      this.currentUrl = url;
      this.getChannelTypeUrl(url);
    },
  },
};
</script>

<style lang="less" scoped>
.loading {
  width: 125px;
  height: 125px;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  // background: #fff;
  color: #fff;
  z-index: 500;
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 15px;
}
.classify {
  width: 100%;
  height: 100%;
  // padding-bottom: 50px;
  .top_bar {
    width: 100%;
    position: relative;
  
    .fdj {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      right: 27px;
      font-size: 20px;
      color: #bababa;
    }
    .tabs {
      /deep/.van-tabs__nav {
        .van-tab {
          flex: 0 0 50px;
          margin-left: 7px;
          padding: 0;
        }
        .van-tab__text--ellipsis {
          color: #bababa;
          font-size: 13px;
        }
        .van-tab--active {
          .van-tab__text--ellipsis {
            font-size: 18px;
            color: #4e4e4e;
          }
        }
      }
      .choice_type {
        // height: 72px;
        width: 100%;
        .type {
          display: inline-block;
          position: relative;
          // margin-left: 20px;
          // width: 53px;
          margin: 0 15px 0 15px;
          height: 36px;
          text-align: center;
          line-height: 36px;
          span {
            color: #1d1d1d;
            font-size: 13px;
          }
          &.active {
            color: #ea7860;
          }
          &.active::after {
            content: "";
            position: absolute;
            top: 80%;
            left: 50%;
            transform: translateX(-50%);
            background: #e54120;
            width: 15px;
            height: 3px;
          }
        }
      }
      .book_data {
        width: 100%;
        height: 100%;
        padding-bottom: 50px;
        background: #fff;
        // padding: 0 14px;
        .filter {
          padding: 0 15px;
          height: 36px;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .new_hot {
            font-size: 12px;
            position: relative;
            color: #bababa;
            span {
              margin-right: 10px;
              &.active {
                color: #ea7860;
              }
            }
            &::after {
              position: absolute;
              top: 50%;
              left: 28px;
              transform: translateY(-50%);
              width: 2px;
              height: 10px;
              content: "";
              background-color: #c7c7c7;
            }
          }
          .btn {
            border-radius: 20px;
            width: 40px;
            height: 20px;
            border: 1px solid #bababa;
            font-size: 12px;
            color: #bababa;
            white-space: nowrap;
            display: flex;
            justify-content: center;
            align-items: center;
            .icon {
              transform: scale(0.5);
              .iconfont {
                color: #bababa;
                font-size: 12px;
              }
            }
          }
        }
        ul {
          display: flex;
          flex-wrap: wrap;
          // & li:nth-of-type(3n) {
          //   margin-right: 0;
          // }
          li {
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            width: 33%;
            height: 200px;
            // margin-right: 23px;
            .img {
              width: 100px;
              height: 140px;
              margin: 0 auto;
              box-shadow: 3px 2px 8px -3px #706e6b;
              img {
                width: 100px;
                height: 140px;
                border-radius: 5px;
              }
            }
            .book_name {
              // width: 100%;
              margin: 0 auto;
              p {
                font-size: 13px;
                color: #000000;
                width: 100px;
                white-space: word-break;
                height: 25px;
              }
              span {
                font-size: 12px;
                color: #d4d4d4;
              }
            }
          }
        }
      }
    }
  }
}
</style>